package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MeansLender;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.MeansService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/means/lender")
public class MeansController extends BaseController {

    @Autowired(required = false)
    private MeansService meansService;

    @Value("${pdf.upload.adddrss}")
    private String UPLOAD_PDF;



    @Log(title = "资料导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/import")
    public AjaxResult meanImport(@RequestParam("file") MultipartFile[] multipartFiles) throws IOException {

        File fileDir = new File(UPLOAD_PDF);
        if (!fileDir.exists() && !fileDir.isDirectory()) {
            fileDir.mkdirs();
        }
        multipartFiles[0].transferTo(new File(fileDir, multipartFiles[0].getOriginalFilename()));
        meansService.readPdf(multipartFiles[0].getOriginalFilename());



       MeansLender meansLender=new MeansLender();
        meansLender.setLenderSex("女");  meansLender.setLenderUserIdcard("43423");
        meansLender.setLenderUserAddress("fdfferfer");

        int lender = meansService.insertLender(meansLender);

        return AjaxResult.success(1);
    }


    @Log(title = "试题解析导入", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/examImport")
    public AjaxResult examImport(@RequestParam("file") MultipartFile[] multipartFiles) throws IOException {

        File fileDir = new File(UPLOAD_PDF);
        if (!fileDir.exists() && !fileDir.isDirectory()) {
            fileDir.mkdirs();
        }
        multipartFiles[0].transferTo(new File(fileDir, multipartFiles[0].getOriginalFilename()));

        String filePath = "F:\\pdf\\"+multipartFiles[0].getOriginalFilename();
        // 使用FileInputStream读取Excel文件
        try {

            FileInputStream fis = new FileInputStream(new File(filePath));
            // 创建XSSFWorkbook对象
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            // 获取第一个工作表（Sheet）
            Sheet sheet = workbook.getSheetAt(0);
            // 获取Sheet的第一个行号和最后一个行号hs.getLastRowNum();
            int last = sheet.getPhysicalNumberOfRows()-2;

            for (int j = 1; j <= last; j++) {
                // 从第二行开始读取数据
                Row row = sheet.getRow(j);
                for (int s = 0; s <= 1; s++){
                    try {
                        Cell cell=row.getCell(s);
                        if (cell.getCellType() != CellType.BLANK && (cell.getCellType() != CellType.STRING || cell.getStringCellValue().trim().length() > 0)) {
                            if (s==0){
                                String excela= filterChinese(cell.getStringCellValue());
                                System.out.println(excela);
                            }

                        }
                    }catch (Exception e){
                        System.out.println(j+"行读取错误");
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        MeansLender meansLender=new MeansLender();
        meansLender.setLenderSex("女");  meansLender.setLenderUserIdcard("43423");
        meansLender.setLenderUserAddress("fdfferfer");

        int lender = meansService.insertLender(meansLender);

        return AjaxResult.success(1);
    }

    public static String filterChinese(String input) {
        if (input == null) return "";
        return input.replaceAll("[^\\u4E00-\\u9FFF]+", "");
    }

//    @GetMapping("/list")
//    public TableDataInfo list(MeansLender user)
//    {
//        startPage();
//        List<MeansLender> list = meansService.selectLenderList(user);
//        return getDataTable(list);
//    }
//
//    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:export')")
//    @GetMapping("/export")
//    public AjaxResult export(MeansLender user)
//    {
//        List<MeansLender> list = meansService.selectLenderList(user);
//        ExcelUtil<MeansLender> util = new ExcelUtil<MeansLender>(MeansLender.class);
//        return util.exportExcel(list, "贷款人信息");
//    }

//    /**
//     * 传入图片文件，进行识别操作
//     * @param file  文件数据
//     */
//    @RequestMapping("/actionOcr")
//    @ResponseBody
//    public String actionOcr(MultipartFile file){
//        return meansService.actionOcr(file);
//    }


    public static void main(String[] str){

    }
}
